# -*- coding: utf-8 -*-

from . import controllers
from . import models
from . import wizards

def _create_warehouse_data(env):
    location = env["stock.location"].search([('usage', '=', 'internal'),('active', '=', True),('replenish_location', '=', True)])
    location_id = 0
    if location:
        location_id = location.id
    dest_location = env["stock.location"].search([('usage', '=', 'production'),('active', '=', True)])
    dest_location_id = 0
    if dest_location:
        dest_location_id = dest_location.id
    tmp_picking_type = env['stock.picking.type'].create({
            'name': '制造发料',
            'code': 'issue',
            'sequence_code': 'PI',
            'default_location_src_id':location_id,
            'default_location_dest_id':dest_location_id,
            'sequence_id': env['ir.sequence'].create({
                'code': 'mrp.production.issue',
                'name': 'production_issue_sequence',
                'padding':6
            }).id,
        })
    tmp_picking_typem = env['stock.picking.type'].create({
        'name': '生产配料单',
        'code': 'wmissue',
        'sequence_code': 'WM',
        'default_location_src_id': location_id,
        'default_location_dest_id': dest_location_id,
        'sequence_id': env['ir.sequence'].create({
            'code': 'mrp.production.wmissue',
            'name': 'production_wmissue_sequence',
            'padding': 6
        }).id,
    })

def uninstall_hook(env):
    picking_types = env["stock.picking.type"].search([('code', '=', 'issue')])
    picking_typews = env["stock.picking.type"].search([('code', '=', 'wmissue')])

    # Fail unlink means that the route is used somewhere (e.g. route_id on stock.rule). In this case
    # we don't try to do anything.
    try:
        with env.cr.savepoint():
            picking_types.unlink()
            picking_typews.unlink()
    except:
        pass

